//
//  JobViewController.m
//  ActSistant
//
//  Created by Hieu Nguyen Trung on 2/28/13.
//

#import "JobViewController.h"
#import "JobCell1.h"
#import "JobCell2.h"
#import <QuartzCore/QuartzCore.h>
#import "UIPlaceHolderTextView.h"
#import "ActSettingViewController.h"
#import "ActVoucherViewController.h"
#import "PayrollAgencyViewController.h"
#import "SetRateViewController.h"
#import "AllowancesViewController.h"

@interface JobViewController ()

@end

@implementation JobViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id) initWithScreenType:(JobScreenType)type{
    self = [super init];
    if (self){
        screenType = type;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initializeViews];
    
}

-(void) initializeViews{
    [self.tableView setTableFooterView:[self tableFooterView]];
    
    [self.navigationView.btnBack addTarget:self action:@selector(btnBackClick) forControlEvents:UIControlEventTouchUpInside];
    switch (screenType) {
        case JobCreate:
            [self.progressView setFrame:CGRectMake(25, 44, 275, 9)];
            [self.progressView setProgress:0];
            [self.lblHeaderTitle setText:@"Production Title"];
            [self.lblHeaderTitle setFont:UIFontSystem(18)];
            [self.navigationView.btnRight1 addTarget:self action:@selector(btnSettingClicked) forControlEvents:UIControlEventTouchUpInside];
            break;
        case JobEdit:
            [self.lblHeaderTitle setTextColor:[UIColor whiteColor]];
            [self.navigationView.btnRight1 release];
            self.navigationView.btnRight1 = nil;
            break;
        case JobReview:
            [self.navigationView.btnRight1 release];
            self.navigationView.btnRight1 = nil;
            self.lblHeaderTitle.text = @"Nixon Watches";
            [self.lblHeaderTitle setTextColor:[UIColor whiteColor]];
            self.lblHeaderPrice.textColor = [UIColor whiteColor];
            [self.progressView setProgress:1];
            break;
        default:
            break;
    }
}

-(void) initializeData{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_navigationView release];
    [_tableView release];
    [_headerView release];
    [_lblHeaderTitle release];
    [_lblHeaderPrice release];
    [_progressView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setNavigationView:nil];
    [self setTableView:nil];
    [self setHeaderView:nil];
    [self setLblHeaderTitle:nil];
    [self setLblHeaderPrice:nil];
    [self setProgressView:nil];
    [super viewDidUnload];
}

-(void) viewDidAppear:(BOOL)animated{
    //do nothing
}

#pragma mark - Actions Handle
-(void) btnBackClick{
    [self.navigationController popViewControllerAnimated:true];
}

-(void) btnTypeClicked{
    SetRateViewController *setRateVC = [[[SetRateViewController alloc] initWithScreenType:SelectRateScreen] autorelease];
    [self.navigationController pushViewController:setRateVC animated:true];
}

-(void) btnRateClicked{
    SetRateViewController *setRateVC = [[[SetRateViewController alloc] initWithScreenType:SelectRateScreen] autorelease];
    [self.navigationController pushViewController:setRateVC animated:true];
}

-(void) btnSettingClicked{
    ActSettingViewController *settingVC = [[[ActSettingViewController alloc] init] autorelease];
    [self.navigationController pushViewController:settingVC animated:true];
}

-(void) btnVoucherClicked{
    ActVoucherViewController *voucherVC = [[[ActVoucherViewController alloc] init] autorelease];
    [self.navigationController pushViewController:voucherVC animated:true];
}

-(void) btnAllowancesClicked{
    AllowancesViewController *allowancesVC = [[[AllowancesViewController alloc] init] autorelease];
    [self.navigationController pushViewController:allowancesVC animated:true];
}

-(void) btnPayrollClicked{
    PayrollAgencyViewController *payrollVC = [[[PayrollAgencyViewController alloc] init] autorelease];
    [self.navigationController pushViewController:payrollVC animated:true];
}

#pragma mark - UITableView Methods Implementation
-(int) numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            switch (screenType) {
                case JobEdit:
                case JobCreate:
                    return 6;
                    break;
                default: //job review
                    return 5;
                    break;
            }
        case 1:
            return 3;
            break;
        case 2:
            return 3;
            break;
        default: //section 4
            return 6;
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 3:
            return 28;
            break;
        default:
            return 40;
            break;
    }
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 38;
            break;
        default:
            return 48;
            break;
    }
}

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view;
    UILabel *label;
    UIView *containerView;
    switch (section) {
        case 0:
            view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 38)] autorelease];
            label = [[[UILabel alloc] initWithFrame:CGRectMake(25, 0, 250, 28)] autorelease];
            containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
            break;
        default:
            view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 48)] autorelease];
            label = [[[UILabel alloc] initWithFrame:CGRectMake(25, 0, 250, 28)] autorelease];
            containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 10, 320, 28)] autorelease];
            break;
    }

    [view setBackgroundColor:[UIColor whiteColor]];
    
    [containerView setBackgroundColor:[UIColor colorWithRed:1 green:86/255.0 blue:86/255.0 alpha:1]];
    
    [label setBackgroundColor:[UIColor clearColor]];
    [label setTextColor:[UIColor whiteColor]];
    [label setFont:UIFontSystemBold(16)];
    switch (section) {
        case 0:
            [label setText:@"Basic Information"];
            break;
        case 1:
            [label setText:@"Breaks"];
            break;
        case 2:
            [label setText:@"Financial"];
            break;
        case 3:
            [label setText:@"Production Information"];
            break;
        default:
            break;
    }
    
    [containerView addSubview:label];
    [view addSubview:containerView];
    
    return view;
}

-(UIView *) tableFooterView{
    UIPlaceHolderTextView *textView = [[[UIPlaceHolderTextView alloc] initWithFrame:CGRectMake(10, 10, 300, 150)] autorelease];
    switch (screenType) {
        case JobCreate:
        case JobEdit:
            [textView setPlaceholder:@"Notes:"];
            break;
        case JobReview:
            [textView setPlaceholder:@"Notes:Shoot included period makeup pieces. Considered for nomination for annie awards."];
        default:
            break;
    }
    [textView.placeHolderLabel setFont:UIFontSystem(9)];
    [textView.layer setBorderWidth:1];
    [textView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [textView.layer setCornerRadius:8];
    [textView setDelegate:self];
    
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)] autorelease];
    [view addSubview:textView];
    
    return view;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 48;
    if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
-  (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    
    switch (screenType) {
        case JobCreate:
            cell = [self cellForCreateScreenAtIndexPath:indexPath];
            break;
        case JobEdit:
            cell = [self cellForEditScreenAtIndexPath:indexPath];
            break;
        default:
            cell = [self cellForReviewScreenAtIndexPath:indexPath];
            break;
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
}

-(UITableViewCell *) cellForEditScreenAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    
    NSArray *objectsInXIB = [[NSBundle mainBundle] loadNibNamed:@"JobCells" owner:self options:nil];
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                case 1:
                case 2:
                case 3:
                    cell = objectsInXIB[0];
                    [((JobCell1 *)cell).cellImageView setImage:[UIImage imageNamed:BASIC_INFO_IMAGES_NAME_ARRAY[indexPath.row]]];
                    [((JobCell1 *)cell).cellTextView setText:BASIC_INFO_STRINGS_ARRAY[indexPath.row]];
                    break;
                case 4:
                    cell = objectsInXIB[1];
                    [((JobCell2 *)cell).buttonTitle setText:BASIC_INFO_STRINGS_ARRAY[indexPath.row]];
                    ((JobCell2 *)cell).isWarning = false;
                    [self setCommonAttributesForCell2Button:((JobCell2 *)cell).cellButton];
                    [((JobCell2 *)cell).cellButton addTarget:self action:@selector(btnTypeClicked) forControlEvents:UIControlEventTouchUpInside];
                    break;
                case 5:
                    cell = objectsInXIB[1];
                    [((JobCell2 *)cell).buttonTitle setText:BASIC_INFO_STRINGS_ARRAY[indexPath.row]];
                    ((JobCell2 *)cell).isWarning = true;
                    [self setCommonAttributesForCell2Button:((JobCell2 *)cell).cellButton];
                    [((JobCell2 *)cell).cellButton addTarget:self action:@selector(btnRateClicked) forControlEvents:UIControlEventTouchUpInside];
                    break;
                default:
                    break;
            }
            break;
            //section 0
        case 1:
            cell = objectsInXIB[0];
            [((JobCell1 *)cell).cellTextView setText:BREAKS_STRINGS_ARRAY[indexPath.row]];
            [((JobCell1 *)cell).cellImageView setImage:[UIImage imageNamed:BREAKS_IMAGES_NAME_ARRAY[indexPath.row]]];
            break;
            //section 1
        case 2:
            cell = objectsInXIB[1];
            [((JobCell2 *)cell).buttonTitle setText:FINANCIAL_STRINGS_ARRAY[indexPath.row]];
            ((JobCell2 *)cell).isWarning = true;
            [self setCommonAttributesForCell2Button:((JobCell2 *)cell).cellButton];
            
            switch (indexPath.row) {
                case 0:
                    [((JobCell2 *)cell).cellButton addTarget:self action:@selector(btnAllowancesClicked) forControlEvents:UIControlEventTouchUpInside];
                    break;
                case 1:
                    [((JobCell2 *)cell).cellButton addTarget:self action:@selector(btnPayrollClicked) forControlEvents:UIControlEventTouchUpInside];
                    break;
                case 2:
                    [((JobCell2 *)cell).cellButton addTarget:self action:@selector(btnVoucherClicked) forControlEvents:UIControlEventTouchUpInside];
                    break;
                default:
                    break;
            }
            //section 2
            break;
        case 3:
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
            
            UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(25, 0, 125, 28)] autorelease];
            [label setTextColor:[UIColor darkGrayColor]];
            [label setFont:UIFontSystem(11)];
            [label setText:PRODUCT_INFO_STRINGS_ARRAY[indexPath.row]];
            
            UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(155, 4, 140, 20)] autorelease];
            [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
            [textField setBorderStyle:UITextBorderStyleBezel | UITextBorderStyleRoundedRect];
            [textField setTextColor:[UIColor darkGrayColor]];
            [textField setFont:UIFontSystem(11)];
            [textField setDelegate:self];
            
            [cell addSubview:textField];
            [cell addSubview:label];
            break;
        default:
            break;
    }
    return cell;
}

-(UITableViewCell *) cellForReviewScreenAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    
    NSArray *objectsInXIB = [[NSBundle mainBundle] loadNibNamed:@"JobCells" owner:self options:nil];
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                    cell = objectsInXIB[0];
                    [((JobCell1 *)cell).cellImageView setImage:[UIImage imageNamed:REVIEW_BASIC_IMAGES_NAME_ARRAY[indexPath.row]]];
                    [((JobCell1 *)cell).cellTextView setText:REVIEW_BASIC_INFO_STRINGS_ARRAY[indexPath.row]];
                    break;
                default:
                    break;
            }
            break;
            //section 0
        case 1:
            cell = objectsInXIB[0];
            [((JobCell1 *)cell).cellTextView setText:REVIEW_BREAKS_STRINGS_ARRAY[indexPath.row]];
            [((JobCell1 *)cell).cellImageView setImage:[UIImage imageNamed:REVIEW_BREAKS_IMAGES_NAME_ARRAY[indexPath.row]]];
            break;
            //section 1
        case 2:
            cell = objectsInXIB[1];
            [((JobCell2 *)cell).buttonTitle setText:FINANCIAL_STRINGS_ARRAY[indexPath.row]];
            ((JobCell2 *)cell).isWarning = false;
            [self setCommonAttributesForCell2Button:((JobCell2 *)cell).cellButton];
            //section 2
            if (indexPath.row == 2){
                [((JobCell2 *)cell).cellButton addTarget:self action:@selector(btnVoucherClicked) forControlEvents:UIControlEventTouchUpInside];
            }
            break;
        case 3:
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
            
            UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(25, 0, 250, 28)] autorelease];
            [label setTextColor:[UIColor darkGrayColor]];
            [label setFont:UIFontSystem(11)];
            [label setText:PRODUCT_INFO_STRINGS_ARRAY[indexPath.row]];
            
            [cell addSubview:label];
            break;
        default:
            break;
    }
    return cell;
}

-(UITableViewCell *) cellForCreateScreenAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    
    NSArray *objectsInXIB = [[NSBundle mainBundle] loadNibNamed:@"JobCells" owner:self options:nil];
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                case 1:
                case 2:
                case 3:
                    cell = objectsInXIB[0];
                    [((JobCell1 *)cell).cellImageView setImage:[UIImage imageNamed:CREATE_BASIC_INFO_IMAGES_NAME_ARRAY[indexPath.row]]];
                    if (indexPath.row != 0){
                        [((UIPlaceHolderTextView *) ((JobCell1 *)cell).cellTextView) setPlaceholder:CREATE_BASIC_INFO_STRINGS_ARRAY[indexPath.row]];
                        [((UIPlaceHolderTextView *) ((JobCell1 *)cell).cellTextView).placeHolderLabel setFont:UIFontSystem(11)];
                    }
                    else{
                        [((JobCell1 *)cell).cellTextView setText:CREATE_BASIC_INFO_STRINGS_ARRAY[indexPath.row]];
                    }
                    break;
                case 4:
                case 5:
                    cell = objectsInXIB[1];
                    [((JobCell2 *)cell).buttonTitle setText:CREATE_BASIC_INFO_STRINGS_ARRAY[indexPath.row]];
                    ((JobCell2 *)cell).isWarning = true;
                    [self setCommonAttributesForCell2Button:((JobCell2 *)cell).cellButton];
                    if (indexPath.row == 4){
                        [((JobCell2 *)cell).cellButton addTarget:self action:@selector(btnTypeClicked) forControlEvents:UIControlEventTouchUpInside];
                    }
                    else{
                        [((JobCell2 *)cell).cellButton addTarget:self action:@selector(btnRateClicked) forControlEvents:UIControlEventTouchUpInside];    
                    }
                    break;
                default:
                    break;
            }
            break;
            //section 0
        case 1:
            cell = objectsInXIB[0];
            [((JobCell1 *)cell).cellTextView setText:CREATE_BREAKS_STRINGS_ARRAY[indexPath.row]];
            [((JobCell1 *)cell).cellImageView setImage:[UIImage imageNamed:CREATE_BREAKS_IMAGES_NAME_ARRAY[indexPath.row]]];
            break;
            //section 1
        case 2:
            cell = objectsInXIB[1];
            [((JobCell2 *)cell).buttonTitle setText:FINANCIAL_STRINGS_ARRAY[indexPath.row]];
            ((JobCell2 *)cell).isWarning = true;
            [self setCommonAttributesForCell2Button:((JobCell2 *)cell).cellButton];
            switch (indexPath.row) {
                case 0:
                    [((JobCell2 *)cell).cellButton addTarget:self action:@selector(btnAllowancesClicked) forControlEvents:UIControlEventTouchUpInside];
                    break;
                case 1:
                    [((JobCell2 *)cell).cellButton addTarget:self action:@selector(btnPayrollClicked) forControlEvents:UIControlEventTouchUpInside];
                    break;
                case 2:
                    [((JobCell2 *)cell).cellButton addTarget:self action:@selector(btnVoucherClicked) forControlEvents:UIControlEventTouchUpInside];
                    break;
                default:
                    break;
            }
            //section 2
            break;
        case 3:
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
            
            UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(25, 0, 125, 28)] autorelease];
            [label setTextColor:[UIColor darkGrayColor]];
            [label setFont:UIFontSystem(11)];
            [label setText:PRODUCT_INFO_STRINGS_ARRAY[indexPath.row]];
            
            UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(155, 4, 140, 20)] autorelease];
            [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
            [textField setBorderStyle:UITextBorderStyleBezel | UITextBorderStyleRoundedRect];
            [textField setTextColor:[UIColor darkGrayColor]];
            [textField setFont:UIFontSystem(11)];
            [textField setDelegate:self];
            
            [cell addSubview:textField];
            [cell addSubview:label];
            break;
        default:
            break;
    }
    return cell;
}

-(void) setCommonAttributesForCell2Button:(UIButton *)button{
    [button.layer setCornerRadius:5];
    [button.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [button.layer setBorderWidth:1];
}

#pragma mark - UITextFieldDelegate Methods Implementation
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return true;
}

#pragma mark - UITextViewDelegate Methods Implementation
-(BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}

@end
